import { defineStore } from 'pinia'
import { reactive } from 'vue'

export const useEnvironmentDataStore = defineStore('environmentData', () => {
  // 初始状态（所有列表为空数组）
  const environmentData = reactive<API.EnvironmentData>({
    dataTimestamp: [],
    indoorTemperature: [],
    outdoorTemperature: [],
    moisture: [],
    soilFertilizerConcentration: [],
    soilPh: [],
    illumination: [],
    conductivity: [],
    potassium: [],
    nitrogen: [],
    phosphorus: [],
  })

  function getDataTimestamp() {
    return environmentData.dataTimestamp
  }

  function getIndoorTemperature() {
    return environmentData.indoorTemperature
  }

  function getOutdoorTemperature() {
    return environmentData.outdoorTemperature
  }

  function getMoisture() {
    return environmentData.moisture
  }

  function getSoilFertilizerConcentration() {
    return environmentData.soilFertilizerConcentration
  }

  function getSoilPh() {
    return environmentData.soilPh
  }

  function getIllumination() {
    return environmentData.illumination
  }

  function getConductivity() {
    return environmentData.conductivity
  }

  function getPotassium() {
    return environmentData.potassium
  }

  function getNitrogen() {
    return environmentData.nitrogen
  }

  function getPhosphorus() {
    return environmentData.phosphorus
  }

  function setDataTimestamp(dataTimestamp: string[]) {
    environmentData.dataTimestamp = dataTimestamp
  }

  function setIndoorTemperature(indoorTemperature: number[]) {
    environmentData.indoorTemperature = indoorTemperature
  }

  function setOutdoorTemperature(outdoorTemperature: number[]) {
    environmentData.outdoorTemperature = outdoorTemperature
  }

  function setMoisture(moisture: number[]) {
    environmentData.moisture = moisture
  }

  function setSoilFertilizerConcentration(soilFertilizerConcentration: number[]) {
    environmentData.soilFertilizerConcentration = soilFertilizerConcentration
  }
  function setSoilPh(soilPh: number[]) {
    environmentData.soilPh = soilPh
  }
  function setIllumination(illumination: number[]) {
    environmentData.illumination = illumination
  }
  function setConductivity(conductivity: number[]) {
    environmentData.conductivity = conductivity
  }
  function setPotassium(potassium: number[]) {
    environmentData.potassium = potassium
  }
  function setNitrogen(nitrogen: number[]) {
    environmentData.nitrogen = nitrogen
  }
  function setPhosphorus(phosphorus: number[]) {
    environmentData.phosphorus = phosphorus
  }

  function resetEnvironmentData() {
    // 直接修改响应式对象的属性（保持响应性）
    Object.assign(environmentData, {
      dataTimestamp: [],
      indoorTemperature: [],
      outdoorTemperature: [],
      moisture: [],
      soilFertilizerConcentration: [],
      soilPh: [],
      illumination: [],
      conductivity: [],
      potassium: [],
      nitrogen: [],
      phosphorus: [],
    })
  }

  return {
    getDataTimestamp,
    getIndoorTemperature,
    getOutdoorTemperature,
    getMoisture,
    getSoilFertilizerConcentration,
    getSoilPh,
    getIllumination,
    getConductivity,
    getPotassium,
    getNitrogen,
    getPhosphorus,
    setDataTimestamp,
    setIndoorTemperature,
    setOutdoorTemperature,
    setMoisture,
    setSoilFertilizerConcentration,
    setSoilPh,
    setIllumination,
    setConductivity,
    setPotassium,
    setNitrogen,
    setPhosphorus,
    resetEnvironmentData,
  }
})
